package test2.spider.news.tencent;

import org.apache.commons.collections.CollectionUtils;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

import jodd.datetime.JDateTime;
import test2.spider.BaseProcessor;

public class DetailProcessor extends BaseProcessor {
	
	private String url;
	
	@Override
	protected String fetch(String url, String encoding) {
		this.url = url;
		return super.fetch(url, encoding);
	}

	@Override
	protected void parse(Document doc) {
		String title = doc.select(".hd").select("h1").text();
		
		Element root = doc.select(".l1").first();
		String channel = root.select("span[bosszone=ztTopic]").select("a").text().trim();
		String source = root.select("span[bosszone=jgname]").text().trim();
		Elements auth = root.select(".auth");
		String author = "佚名";
		if(CollectionUtils.isNotEmpty(auth)) {
			author = auth.select("a").text().trim();
		}
		String pubtime = root.select(".pubTime").text().trim();
		
		System.err.println(pubtime);
		System.err.println(new JDateTime().toString("YYYY-MM-DD hh:mm"));
		boolean isold = new JDateTime(pubtime, "YYYY-MM-DD hh:mm").isBeforeDate(new JDateTime());
		System.err.println(isold);
		if(isold) return;
		
		String content = doc.getElementById("Cnt-Main-Article-QQ").select("p").outerHtml().trim();
		
		Record news = new Record();
		news.set("title", title)
			.set("source", source)
			.set("author", author)
			.set("content", content)
			.set("pubtime", pubtime)
			.set("type", "腾讯")
			.set("channel", channel)
			.set("url", url);
		Db.save("news2", news);
	}

}
